<?php
namespace app\admin\controller;

/**
 * Created by PhpStorm.
 * User: Liulibao
 * Date: 2019/10/10
 * Time: 21:57
 */

use app\common\model\AfficheTypeModel;
use think\Lang;

class Affichetype extends AdminControl
{
    protected $afficheType;

    /**
     * @description:   @MrLiu
     */
    public function _initialize()
    {
        parent::_initialize();
        Lang::load(APP_PATH . 'admin/lang/' . config('default_lang') . '/affiche.lang.php');
    }

    public function index()
    {
        $afficheType = model('afficheTypeModel');
        $list =  $afficheType->getAfficheTypeList([], 7);

        $this->assign('list', $list);
        $this->assign('show_page', $afficheType->page_info->render());

        $this->setAdminCurItem('index');
        return $this->fetch('affiche/type_index');
    }

    public function add()
    {
        $this->setAdminCurItem('add');
        return $this->fetch('affiche/type_edit');
    }

    /**
     * @description: 编辑 @MrLiu
     */
    public function edit(AfficheTypeModel $afficheTypeModel)
    {
        $id = intval(input('param.id'));
        $data = $afficheTypeModel->getOneAfficheTypeById($id);

        $this->assign('data', $data);
        $this->setAdminCurItem('edit');
        return $this->fetch('affiche/type_edit');
    }

    /**
     * @description: 更新或者保存 @MrLiu
     */
    public function save(AfficheTypeModel $afficheTypeModel)
    {
        $id = intval(input('param.id'));
        $data = array(
            'name' =>   input('param.name'),
        );
        $validate = validate('affichetype');
        if (!$validate->scene('affichetype')->check($data)) {
            $this->error($validate->getError());
        }

        if ($this->check_type_name($data['name'])) {
            $this->error(lang('affiche_type_add_name_exists'));
        }

        if ($id) {
            $status = $afficheTypeModel->editAfficheType($data, $id);
        } else {
            $status =$afficheTypeModel->addAfficheType($data);
        }

        if ($status>=0) {
            $this->log(lang('ds_edit') . lang('affiche_type_add') . '[' . $data['name'] . ']', 1);
            dsLayerOpenSuccess(lang('affiche_edit_success'));
        } else {
            $this->error(lang('affiche_edit_fail'));
        }
    }


    /**
     * @description: 删除操作  @MrLiu
     */
    public function cancels(AfficheTypeModel $afficheTypeModel)
    {
        $id = intval(input('param.id'));
        $status = $afficheTypeModel->delAfficheType($id);
        if ($status === false) {
            ds_json_encode(10001, lang('affiche_del_exists'));
        }
        $this->log(lang('ds_add') . lang('affiche_type_index_class') . '[ID:' . $id . ']', 1);
        ds_json_encode(10000, lang('affiche_del_success'));
    }

    /**
     * @description: 检车名称是否存在  @MrLiu
     * @return bool
     */
    protected function check_type_name($name)
    {
        $afficheTypeModel = new AfficheTypeModel();
        $data = $afficheTypeModel->getOneAfficheType(array('name' => $name));
        if (!empty($data)) {
            return true;
        } else {
           return false;
        }
    }

    /**
     * @description:   @MrLiu
     * @return array
     */
    protected function getAdminItemList()
    {
        $menu_array = array(
            array(
                'name' => 'index',
                'text' => lang('ds_manage'),
                'url' => url('Affichetype/index')
            ),
        );

        if (request()->action() == 'add' || request()->action() == 'index') {
            $menu_array[] = [
                'name' => 'add',
                'text' => lang('ds_new'),
                'url' => "javascript:dsLayerOpen('" . url('Affichetype/add') . "','" . lang('affiche_type_add') . "')",
            ];
        }

        if (request()->action() == 'edit') {
            $menu_array[] = array(
                'name' => 'edit',
                'text' => lang('ds_edit'),
                'url' => 'javascript:void(0)'
            );
        }
        return $menu_array;
    }
}